package com.yixun.qfbao.response.customer;

import com.yixun.qfbao.model.Customer;
import com.yixun.qfbao.model.dto.CustomerDto;
import com.yixun.qfbao.model.enums.*;
import com.yixun.qfbao.xf.model.CustomerPremises;
import com.yixun.qfbao.xf.model.enums.ConfirmStatusEnum;
import com.yixun.qfbao.xf.model.enums.CustomerStatusEnum;
import com.yixun.qfbao.xf.model.enums.EstateTypeEnum;
import com.yixun.qfbao.xf.model.enums.GenderEnum;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import java.util.Map;
import java.util.Objects;

public class CustomerConvert {

    public static CustomerPageResDto convertCustomer2PageResDto(Customer customer, Map<String, String> intentionPremisesMap){
        CustomerPageResDto resDto = new CustomerPageResDto();
        if(Objects.isNull(customer)){
            return null;
        }

        resDto.setId(customer.getId());
        resDto.setRealName(customer.getRealName());
        resDto.setMainMobile(customer.getMainMobile());
        resDto.setMinorMobile(customer.getMinorMobile());
        resDto.setThirdMobile(customer.getThirdMobile());
        String genderCN = "";
        if(StringUtils.isNotBlank(customer.getGender())){
            genderCN = GenderEnum.getDescByNo(customer.getGender());
        }
        resDto.setGenderCN(genderCN);
        resDto.setHouseTypeCN(getHouseTypeCN(customer.getHouseType()));
        resDto.setAreaMin(customer.getAreaMin());
        resDto.setAreaMax(customer.getAreaMax());
        resDto.setAreaCN(getAreaCN(customer.getAreaMin(), customer.getAreaMax()));
        if(StringUtils.isNotBlank(customer.getPayMethod())){
            resDto.setPayMethodCN(PayMethodEnum.getNameByCode(customer.getPayMethod()));
        }
        resDto.setBudgetMin(customer.getBudgetMin());
        resDto.setBudgetMax(customer.getBudgetMax());
        resDto.setBudgetCN(getBudgetCN(customer.getBudgetMin(), customer.getBudgetMax()));
        resDto.setDownBudgetMin(customer.getDownBudgetMin());
        resDto.setDownBudgetMax(customer.getDownBudgetMax());
        resDto.setDownBudgetCN(getDownBudgetCN(customer.getDownBudgetMin(), customer.getDownBudgetMax()));
        resDto.setIntentionPremisesName(customer.getIntentionPremisesName());
        if(Objects.nonNull(intentionPremisesMap) && !intentionPremisesMap.isEmpty()
                && StringUtils.isNotBlank(intentionPremisesMap.get(String.valueOf(customer.getId())))){
            resDto.setIntentionPremisesName(intentionPremisesMap.get(String.valueOf(customer.getId())));
        }
        return resDto;
    }

    public static CustomerDto convertCustomer2Dto(Customer customer){
        CustomerDto customerDto = new CustomerDto();
        if(Objects.isNull(customer)){
            return null;
        }
        customerDto.setId(customer.getId());
        customerDto.setAgentCompany(customer.getAgentCompany());
        customerDto.setAgentId(customer.getAgentId());
        customerDto.setOriginalReport(customer.getOriginalReport());
        customerDto.setRealName(customer.getRealName());
        customerDto.setGender(customer.getGender());
        String genderCN = "";
        if(StringUtils.isNotBlank(customer.getGender())){
            genderCN = GenderEnum.getDescByNo(customer.getGender());
        }
        customerDto.setGenderCN(genderCN);
        customerDto.setMainMobile(customer.getMainMobile());
        customerDto.setMinorMobile(customer.getMinorMobile());
        customerDto.setThirdMobile(customer.getThirdMobile());
        customerDto.setTelephone(customer.getTelephone());
        customerDto.setAgeRange(customer.getAgeRange());
        String ageRangeCN = "";
        if(StringUtils.isNotBlank(customer.getAgeRange())){
            ageRangeCN = AgeRangeEnum.getNameByCode(customer.getAgeRange());
        }
        customerDto.setAgeRangeCN(ageRangeCN);
        customerDto.setBudgetMin(customer.getBudgetMin());
        customerDto.setBudgetMax(customer.getBudgetMax());
        customerDto.setBudgetCN(getBudgetCN(customer.getBudgetMin(), customer.getBudgetMax()));
        customerDto.setDownBudgetMin(customer.getDownBudgetMin());
        customerDto.setDownBudgetMax(customer.getDownBudgetMax());
        customerDto.setDownBudgetCN(getDownBudgetCN(customer.getDownBudgetMin(), customer.getDownBudgetMax()));
        customerDto.setAreaMin(customer.getAreaMin());
        customerDto.setAreaMax(customer.getAreaMax());
        customerDto.setHouseType(customer.getHouseType());
        customerDto.setHouseTypeCN(getHouseTypeCN(customer.getHouseType()));
        customerDto.setFloor(customer.getFloor());
        customerDto.setFloorCN(getFloorCN(customer.getFloor()));
        customerDto.setDecoration(customer.getDecoration());
        String decorationCN = "";
        if(StringUtils.isNotBlank(customer.getDecoration())){
            decorationCN = DecorationTypeEnum.getNameByCode(customer.getDecoration());
        }
        customerDto.setDecorationCN(decorationCN);
        customerDto.setPropertyType(customer.getPropertyType());
        customerDto.setPropertyTypeCN(getPropertyTypeCN(customer.getPropertyType()));
        customerDto.setPeopleCount(customer.getPeopleCount());
        String peopleCountCN = "";
        if(StringUtils.isNotBlank(customer.getPeopleCount())){
            peopleCountCN = PeopleCountEnum.getNameByCode(customer.getPeopleCount());
        }
        customerDto.setPeopleCountCN(peopleCountCN);
        customerDto.setIntentionArea(customer.getIntentionArea());
        customerDto.setIntentionAreaName(customer.getIntentionAreaName());
        customerDto.setIntentionPremises(customer.getIntentionPremises());
        customerDto.setIntentionPremisesName(customer.getIntentionPremisesName());
        customerDto.setHousePurpose(customer.getHousePurpose());
        String housePurposeCN = "";
        if(StringUtils.isNotBlank(customer.getHousePurpose())){
            housePurposeCN = HousePurposeEnum.getNameByCode(customer.getHousePurpose());
        }
        customerDto.setHousePurposeCN(housePurposeCN);
        customerDto.setPayMethod(customer.getPayMethod());
        String payMethodCN = "";
        if(StringUtils.isNotBlank(customer.getPayMethod())){
            payMethodCN = PayMethodEnum.getNameByCode(customer.getPayMethod());
        }
        customerDto.setPayMethodCN(payMethodCN);
        customerDto.setRemark(customer.getRemark());
        customerDto.setCreateTime(customer.getCreateTime());
        return customerDto;
    }

    private static String[] getFloorCN(String floor){
        if(StringUtils.isNotBlank(floor)){
            String [] floorArr = floor.split(",");
            String[] floorCNArr = new String[floorArr.length];
            for(int ii=0; ii<floorArr.length; ii++){
                floorCNArr[ii] = FloorEnum.getNameByCode(floorArr[ii]);
            }
            return floorCNArr;
        }
        return null;
    }

    private static String[] getHouseTypeCN(String houseType){
        if(StringUtils.isNotBlank(houseType)){
            String [] houseTypeArr = houseType.split(",");
            String[] houseTypeCNArr = new String[houseTypeArr.length];
            for(int ii=0; ii<houseTypeArr.length; ii++){
                houseTypeCNArr[ii] = HuxingEnum.getNameByCode(houseTypeArr[ii]);
            }
            return houseTypeCNArr;
        }
        return null;
    }

    private static String[] getPropertyTypeCN(String propertyType){
        if(StringUtils.isNotBlank(propertyType)){
            String [] propertyTypeArr = propertyType.split(",");
            String[] propertyTypeCNArr = new String[propertyTypeArr.length];
            for(int ii=0; ii<propertyTypeArr.length; ii++){
                propertyTypeCNArr[ii] = EstateTypeEnum.getNameByCode(propertyTypeArr[ii]);
            }
            return propertyTypeCNArr;
        }
        return null;
    }

    private static String getAreaCN(String areaMix, String areaMax){
        String areaCN = areaMix;
        if(StringUtils.isNotBlank(areaCN) && StringUtils.isNotBlank(areaMax)){
            areaCN += "-" + areaMax + "㎡";
        }else if(StringUtils.isNotBlank(areaCN) && StringUtils.isBlank(areaMax)){
            areaCN += "㎡";
        }else if(StringUtils.isBlank(areaCN) && StringUtils.isNotBlank(areaMax)){
            areaCN = areaMax + "㎡";
        }
        return areaCN;
    }

    private static String getBudgetCN(String budgetMin, String budgetMax){
        String budgetCN = budgetMin;
        if(StringUtils.isNotBlank(budgetCN) && StringUtils.isNotBlank(budgetMax)){
            budgetCN += "-" + budgetMax + "万元";
        }else if(StringUtils.isNotBlank(budgetCN) && StringUtils.isBlank(budgetMax)){
            budgetCN += "万元";
        }else if(StringUtils.isBlank(budgetCN) && StringUtils.isNotBlank(budgetMax)){
            budgetCN = budgetMax + "万元";
        }
        return budgetCN;
    }

    private static String getDownBudgetCN(String downBudgetMin, String downBudgetMax){
        String downBudgetCN = downBudgetMin;
        if(StringUtils.isNotBlank(downBudgetCN) && StringUtils.isNotBlank(downBudgetMax)){
            downBudgetCN += "-" + downBudgetMax + "万元";
        }else if(StringUtils.isNotBlank(downBudgetCN) && StringUtils.isBlank(downBudgetMax)){
            downBudgetCN += "万元";
        }else if(StringUtils.isBlank(downBudgetCN) && StringUtils.isNotBlank(downBudgetMax)){
            downBudgetCN = downBudgetMax + "万元";
        }
        return downBudgetCN;
    }

    public static CustomerHisReportResDto convertCP2HisReport(CustomerPremises customerPremises){
        CustomerHisReportResDto hisReportResDto = new CustomerHisReportResDto();
        if(Objects.isNull(customerPremises)){
            return null;
        }
        hisReportResDto.setId(customerPremises.getId());
        hisReportResDto.setPremisesId(customerPremises.getPremisesId());
        hisReportResDto.setPremisesName(customerPremises.getPremisesName());
        if(StringUtils.isNotBlank(customerPremises.getCustomerStatus())){
            hisReportResDto.setCustomerStatus(customerPremises.getCustomerStatus());
            hisReportResDto.setCustomerStatusCN(CustomerStatusEnum.getDescByNo(customerPremises.getCustomerStatus()));
        }
        if(StringUtils.isNotBlank(customerPremises.getConfirmStatus())){
            hisReportResDto.setConfirmStatus(customerPremises.getConfirmStatus());
            hisReportResDto.setConfirmStatusCN(ConfirmStatusEnum.getDescByNo(customerPremises.getConfirmStatus()));
        }
        hisReportResDto.setCreateTime(customerPremises.getCreateTime());
        return hisReportResDto;
    }

}
